@include('admin.public.head')
@include('admin.public.pop')

<style type="text/css">
    .inner_btn {
        background: #19a97b;
        color: white;
        padding: 5px 8px;
        border-radius: 2px;
    }
</style>


<div class="ibox-title">
    <form class="form-horizontal">
        <div class="col-sm-2">
            <select class="form-control" name="vip" id="vip" style="float:left">
                <option value="" >請選擇</option>
                @foreach($vip_lists as $v)
                    <option value="{{$v->id}}" >{{$v->name}}&nbsp;&nbsp;有效天數：{{$v->duration}}&nbsp;&nbsp;激活時間：@if(!empty($v->activate)) {{$v->activate}} @else 無限制 @endif</option>
                @endforeach
            </select>
        </div>

        <input type="hidden" name="user_id" value="{{$user_id??0}}">

        <button class="btn btn-primary do" type="submit">購買vip</button>
    </form>
</div>

<div class="ibox-content">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>vip</th>
            <th>到期時間</th>
            <th>激活時間</th>
            <th>編輯</th>
        </tr>
        </thead>
        <tbody>
        @foreach($user_vip as $k => $v)
            <tr>
                <td>{{$v->vip_title??''}}</td>
                <td>
                    <div class="form-group">

                        <a class="btn btn-primary btn-sm dropdown-toggle" onclick="save_time($(this))"
                           vip_id="{{$v->vip_id}}" style="height: 34px;line-height: 23px;" href="javascript:void(0)">保存</a>

                        <div class="col-sm-7">
                            <input type="text" name="expire_time_{{$v->vip_id}}" class="form-control" style="float: left"
                                   id="expire_time_{{$k}}" value="{{$v->vip_maturity??''}}">
                        </div>

                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <a class="btn btn-primary btn-sm dropdown-toggle" onclick="activate_time($(this))"
                           vip_id="{{$v->vip_id}}" style="height: 34px;line-height: 23px;" href="javascript:void(0)">保存</a>

                        <div class="col-sm-7">
                            <input type="text" name="activate_{{$v->vip_id}}" class="form-control" style="float: left"
                                   id="activate_{{$k}}" value="{{$v->activate??''}}">
                        </div>

                    </div>
                </td>
                <td>
                    <div class="btn-group">
                        <a class="btn btn-primary btn-sm" href="/user_vip_info/{{$user_id}}/{{$v->vip_id}}"
                          >詳細
                        </a>
                    </div>
                    <div class="btn-group">
                        <a class="btn btn-primary btn-sm" href="/user_vip_use_info/{{$user_id}}/{{$v->vip_id}}"
                        >使用記錄
                        </a>
                    </div>
                    <div class="btn-group">
                        <a class="btn btn-primary btn-sm dropdown-toggle" href="javascript:void(0)" onclick="del($(this))" url="/user_vip_del/{{$user_id}}/{{$v->vip_id}}">刪除
                        </a>&nbsp;&nbsp;
                    </div>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>

<script>

    var count = "{{count($user_vip)}}";

    for (var i = 0; i < count; i++) {

        laydate.render({
            elem: '#expire_time_' + i,
            min: '', //设定最小日期为当前日期
            max: '2099-06-16 23:59:59', //最大日期
            istime: false,
            istoday: false,
            choose: function (datas) {
                //end.min = datas; //开始日选好后，重置结束日的最小日期
                //end.start = datas //将结束日的初始值设定为开始日
            }
        });

        laydate.render({
            elem: '#activate_' + i,
            min: '', //设定最小日期为当前日期
            max: '2099-06-16 23:59:59', //最大日期
            istime: false,
            istoday: false,
            choose: function (datas) {
                //end.min = datas; //开始日选好后，重置结束日的最小日期
                //end.start = datas //将结束日的初始值设定为开始日
            }
        });

    }

    function activate_time(obj) {
        var vip_id = obj.attr('vip_id');

        var user_id = "{{$user_id}}";

        var activate = $("input[name='activate_" + vip_id + "']").val();

        var form = new FormData();

        form.append("user_id", user_id);
        form.append("vip_id", vip_id);
        form.append("activate", activate);

        $.ajax({
            type: 'POST',
            url: '/user_update_vip_activate_time',
            dataType: "json",   //返回格式为json
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {

                if (data.status == '1') {
                    var swal_json = {
                        title: data.message,
                        type: "success"
                    }

                    swal(swal_json, function (isConfirm) {
                        //window.location.reload();
                    });

                } else {
                    var swal_json = {
                        title: data.message,
                        type: "error"
                    }

                    swal(swal_json, function (isConfirm) {

                    });
                }
            }
        });

    }

    function save_time(obj) {
        var vip_id = obj.attr('vip_id');

        var user_id = "{{$user_id}}";

        var time = $("input[name='expire_time_" + vip_id + "']").val();

        var form = new FormData();

        form.append("user_id", user_id);
        form.append("vip_id", vip_id);
        form.append("expire_time", time);

        $.ajax({
            type: 'POST',
            url: '/user_update_vip_expire_time',
            dataType: "json",   //返回格式为json
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {

                if (data.status == '1') {
                    var swal_json = {
                        title: data.message,
                        type: "success"
                    }

                    swal(swal_json, function (isConfirm) {
                        //window.location.reload();
                    });

                } else {
                    var swal_json = {
                        title: data.message,
                        type: "error"
                    }

                    swal(swal_json, function (isConfirm) {

                    });
                }
            }
        });

    }

    $('.do').click(function(){

        var title =  "你確定購買該vip";
        var botton =  "購買";
        var result =  "購買成功";


        swal({
            title: title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: botton,
            closeOnConfirm: false
        }, function () {
            var form = new FormData();

            var vip = $('#vip option:selected').val();
            var user_id = $('input[name="user_id"]').val();

            form.append("vip_id", vip);
            form.append("user_id", user_id);

            $.ajax({
                type: 'POST',
                url: '/user_vip_do' ,
                dataType:"json",   //返回格式为json
                data: form ,
                processData: false,
                contentType: false,
                success:function(data){

                    if(data.status == '1') {
                        var swal_json = {
                            title: data.message,
                            type: "success"
                        }

                        swal(swal_json, function (isConfirm) {
                            window.location.reload();
                        });

                    }else{
                        var swal_json = {
                            title: data.message,
                            type: "error"
                        }

                        swal(swal_json, function (isConfirm) {

                        });
                    }
                }
            });
        });

        return false;
    });
</script>
